<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
    <!-- 导入分页插件 -->
    <script src="./libs/jquery.twbsPagination.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr></tr> -->
                        <!-- <td>王积千造统最头</td></tr> -->
                        <!-- <td>杰克</td></tr> -->
                        <!-- <td>奇趣事</td></tr> -->
                        <!-- <td class="text-center">2017-06-08 07:08:46</td></tr> -->
                        <!-- <td class="text-center">已发布</td></tr> -->
                        <!-- <td class="text-center"></tr> -->
                            <!-- <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a></tr> -->
                            <!-- <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a></tr> -->
                        <!-- </td></tr> -->
                    <!-- </tr></tr> -->
                </tbody>
            </table>
            <div class="row text-center">
                <ul id ="pagination" class="pagination-sm"></ul>
            </div>
        </div>
    </div>
</body>
<script src="./libs/https.js"></script>
<!-- 准备类别选项结构 -->
<script type="text/html" id ="tpl-category">
    {{each data v}}
    <option value="{{v.id}}">{{v.name}}</option>
    {{/each}}
</script>
<script type="text/html" id ="tpl-article">
   {{each data.data v}}
   <tr>
    <td>{{v.title}}</td>
    <td>{{v.author}}</td>
    <td>{{v.category}}</td>
    <td class="text-center">{{v.date}}</td>
    <td class="text-center">{{v.state}}</td>
    <td class="text-center">
        <a href="article_edit.html?id={{v.id}}" class="btn btn-default btn-xs" data-id="{{v.id}}">编辑</a>
        <a href="javascript:void(0);" class="btn btn-danger btn-xs delete" data-id="{{v.id}}">删除</a>
    </td>
</tr>
   {{/each}}
</script>
<script>
    $(function(){
        //1.获得所有文章分类，生成下拉框
        let selPage;
        $.ajax({
            type:'get',
            url:BigNew.category_list,
            success:function(backData){
                if(backData.code ==200){
                    let htmlstr =template('tpl-category',backData)
                   
                    $('#selCategory').html(`<option value=''>所有分类</option>`+htmlstr)
                }
            }
        })
        // 先有数据,才有分页导航
        function loadData(page){
            $.ajax({
                type:'get',
                url:BigNew.article_query,
                data:{
                    type:$('#selCategory').val(),
                    state:$('#selStatus').val(),
                    page:page,
                    perpage:5
                },
                success:function(backData){
                   
                    if(backData.code ==200){
                        if(backData.data.totalCount>0){
                            //结合模板引擎生成标签结构
                        let htmlstr =template('tpl-article',backData)
                        $('tbody').html(htmlstr)
                        //渲染结构之后，加载分页导航
                        $('#pagination').twbsPagination('destroy');
                        $('#pagination').twbsPagination({
                             //设置总页数
                             totalPages:backData.data.totalPage,
                             //设置可见页数
                             visiblePages:10,
                             //当前被选中的页数
                             startPage:page,
                             //关闭默认的点击事件
                             initiateStartPageClick:false,
                             //修改分页导航的按钮文本
                             first: '首页',
                             last: '尾页',
                             next: '下一页',
                             prev: '上一页',
                             //页码的点击事件
                             onPageClick:function(enevt,page){
                                 
                               // page:当前被点击的页码，根据页码获得指定的文章数据
                               loadData(page)
                               selPage =page
                             }
                        })
                        }else{
                             // 没有数据,则清空表格,不要显示分页导航,显示提示文本
                            $('tbody').empty();
                            $('#pagination').html('当前类别没有任何数据...');
                        }
                        
                    }
                    
                }
            })
        }
        loadData(1)
        //给筛选按钮绑定点击事件
        $('#btnSearch').on('click',function(e){
            e.preventDefault();
            loadData(1)
        })
        //给所有删除按钮设置事件
        $('tbody').on('click','.delete',function(){
            let dataid =$(this).attr('data-id');
            console.log(dataid);
            $.ajax({
                type:'post',
                url:BigNew.article_delete,
                data:{
                    id:dataid
                },
                success:function(backData){
                    console.log(backData);
                    if(backData.code == 204){
                        if($('tbody tr').length>1 || selPage == 1){
                            loadData(selPage);
                        }else{
                            selPage = selPage - 1;
                            loadData(selPage); 
                        }
                    }
                }
            })
        })
    })
</script>
</html>